﻿var oldExtendedSetupGrid = extendedSetupGrid;
extendedSetupGrid = function(grid)
{
	oldExtendedSetupGrid(grid);
	var bizProc = eval(eval(grid.ID + "ParentBizProc"));
	if(bizProc != null)
	{
		for(var pos = 0; pos < bizProc.ClientControls.length; pos++)
			if(bizProc.ClientControls[pos].id == grid.ID)
				return;
				
		grid.ClientControl = new ClientControl(grid, setGridState, getGridState);
		grid.ClientControl.id = grid.ID;
		bizProc.addClientControl(grid.ClientControl);
		grid.BizProc = bizProc;
	}
}

function setGridState(stateEl)
{
    var g = this.dhtmlObj;
	if(stateEl.getAttribute('refresh') != null)
	{
	    //this.dhtmlObj.refresh();
		var rc = stateEl.getAttribute('rowCount');
        g.rowCountKnown = false;
        g.clearSurfaces();
        g.refresh(); 
	}
	if(stateEl.getAttribute('rowCount') != null)
	{
		var rc = stateEl.getAttribute('rowCount');
		g.setRowCount(rc);
	}
}

function getGridState(stateEl)
{
	var hiddenField = document.getElementById(this.id + "_SelRowInfo");
	stateEl.setAttribute('selectedRows', hiddenField.value);

//	debugger;
	if(this.dhtmlObj.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
	{
		stateEl.setAttribute('updates',this.dhtmlObj.datatable.log.xml);
		this.dhtmlObj.datatable.flushLog();
	}
}

function gridCellFocus(eventArgs)
{
	var grid = eventArgs.getSource();
    var cell = eventArgs.getCell();
	var row = cell.getRow();	
	if(grid.syrinxLastRow != row)
	{
	    grid.syrinxLastRow = row;
	    if(grid.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
	    {
		    grid.BizProc.triggerEvent(grid.ClientControl.id, 'bizObjUpdated', this.dhtmlObj.datatable.log.xml);
			this.dhtmlObj.datatable.flushLog();
		}
    }
}

function gridCellEditComplete(eventArgs)
{
	try
	{
		if(eventArgs.getEvent().type == "blur" || eventArgs.getEvent().type == "keydown")
		{										   
			var cell = eventArgs.getCell();
			var col = cell.getColumn();
			var row = cell.getRow();
			var grid = eventArgs.getSource();
			var backward = eventArgs.getEvent().shiftKey;
			var keyCode = eventArgs.getEvent().keyCode;
			//var realBlur = true;
			var doUpdate = false;
															   
			//debugger;
			
			if(eventArgs.getEvent().type == "blur")
			{
				var lastKey = -1;
				try{lastKey = grid.cellEditor.lastKeyCode;}catch(e){}
				if(lastKey == 13)
					doUpdate = true;
			}
			else if(keyCode == 38 || keyCode == 40 || keyCode == 13)
				doUpdate = true;
						
			if(doUpdate && grid.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
			{
				grid.BizProc.triggerEvent(grid.ID, 'bizObjUpdated', grid.datatable.log.xml);
				grid.datatable.flushLog();
			}
		}
	}
	catch(e) {}
}
